package com.jiaai.cloud.live.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;

import com.baomidou.mybatisplus.plugins.Page;
import com.jiaai.cloud.base.exception.ServiceException;

import com.jiaai.cloud.base.page.CustomPage;
import com.jiaai.cloud.base.page.FrontPage;
import com.jiaai.cloud.base.util.RestResultGenerator;
import com.jiaai.cloud.live.entity.TAnchor;
import com.jiaai.cloud.live.entity.TAnchorSearch;

import com.jiaai.cloud.live.service.ITAnchorService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 主播基础信息表 前端控制器
 * </p>
 *
 * @author 佳爱网络科技
 * @version V1.0
 * @title TAnchorController.java
 * @since 2017/8/1
 */
@RestController
@RequestMapping("/api/manage/anchor")
public class TAnchorController {
    @Autowired
    private ITAnchorService itAnchorService;
    //可以根据anchorId查找,不传id查所有，带分页
    @RequestMapping(value = "/find", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Object find(TAnchorSearch search) throws ServiceException {
        try {
            Page page =search.getPagePlus();
            EntityWrapper wrapper=new EntityWrapper();
            if(!StringUtils.isEmpty(search.getAnchorId())){
                wrapper.like("g.anchorId",search.getAnchorId());
            }
            return RestResultGenerator.genSuccessResult(itAnchorService.findByAll(page, wrapper));
        } catch (Exception e) {

            throw new ServiceException("查询失敗", e.getMessage());
        }
    }
    //可以根据anchorId查找,不传id查所有，带分页
    @RequestMapping(value = "/findByAll", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Object findByAll(TAnchorSearch search) throws ServiceException {
        try {
            return RestResultGenerator.genSuccessResult(itAnchorService.findAnchorFully(search.getPagePlus(),search));
        } catch (Exception e) {

            throw new ServiceException("查询失敗", e.getMessage());
        }
    }

    //可以根据anchorId查找,主播直播观看人数详情
    @RequestMapping(value = "/findZhiboByAll", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Object findZhiboByAll() throws ServiceException {
        try {
            return RestResultGenerator.genSuccessResult(itAnchorService.findZhiboFully());
        } catch (Exception e) {
            throw new ServiceException("查询失敗", e.getMessage());
        }
    }

    //查所有主播排行
    @RequestMapping(value = "/findanchor", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Object findanchor(FrontPage page) throws ServiceException {
        try {
            EntityWrapper wrapper=new EntityWrapper();
            return RestResultGenerator.genSuccessResult(itAnchorService.anchorPaihang(page.getPagePlus(),wrapper));
        } catch (Exception e) {
            throw new ServiceException("查询失敗", e.getMessage());
        }
    }

    // 通过anchorId删除
    @RequestMapping(value = "/deleteId/{anchorId}", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
    public Object deleteId(@PathVariable("anchorId")  String anchorId) throws ServiceException {
        try {
            itAnchorService.delete(anchorId);
            return RestResultGenerator.genSuccessResult();
        } catch (Exception e) {
            throw new ServiceException("删除失敗", e.getMessage());
        }
    }
    //通过改anchorId改status状态
    @RequestMapping(value = "/changeStatus/{anchorId}/{status}", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Object changeStatus(@PathVariable("anchorId") String anchorId, @PathVariable("status") Integer status) throws ServiceException {
        try {
            TAnchor tAnchor = new TAnchor();
            tAnchor.setAnchorId(anchorId);
            tAnchor.setStatus(status);
            itAnchorService.updateById(tAnchor);
            return RestResultGenerator.genSuccessResult();
        }catch (Exception e) {
            throw new ServiceException("修改失敗", e.getMessage());
        }
    }





	
}


